<?php
## include required files
/*******************************/ 
require_once '../includefiles.php';
require_once '../model/users.php';
require_once '../model/email.php';
require_once '../model/admindetails.php';
require_once '../model/config.php';
/*******************************/

## Create Objects
/*******************************/
$userObj = new Model_Users();
$emailObj=new Model_Email();
$adminObj= new Model_AdminDetails();
$configObj = new Model_Config();
/*******************************/
##fetching the logo
$logoArr=$configObj->getConfigValueById(1);
$smarty->assign('logoArr', $logoArr);

if(isset($_POST['forgot_btn'])){
		## apply PHP validation for required fields whenever you submit the form in your project
		if(trim($_POST['email']) != '') {
			$user_res=$userObj->checkAdminEmailPresent($_POST['email']);
				if(count($user_res) >0){
					// fetching user detail
					$userArray=$userObj->getUserDetailsByUserId($user_res['id']);
					// reset link
					$link= '<a href="'.SITE_URL."/admin/resetpassword.php?id=".md5($userArray['id']).'">'.SITE_URL."/admin/resetpassword.php?id=".md5($userArray['id']).'</a>';
					$username=$userArray['first_name']." ".$userArray['last_name'];
					$emailArray=$emailObj->getEmailById(2);
					$to=$userArray['email'];
					$subject=$emailArray['email_subject'];
					
					$message=$emailArray['email_content'];
					
					$message=str_replace('[NAME]', $username, $message);
					$message=str_replace('[ACTIVATIONURL]', $link, $message);
					$message=str_replace('[SITENAME]',SITENAME , $message);
					$message=str_replace('[SITE_LINK]',SITE_URL , $message);
					
					$from = $emailArray['from_email'];
					$fromname = FRONTTITLE;
					
					
					$emailTemplate=file_get_contents('../emailtemplate.html');
					
					$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
					$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
					$template_msg=str_replace('[MESSAGE]',$message , $template_msg);

					//echo $to; echo $template_msg; echo $subject; echo $from; echo $fromname; exit;
					if(trim($to) != '')
							@send_mail($to, $template_msg, $subject, $from, $fromname);			
							
							

					$_SESSION['msg'] = "<div class='success_msg'><span>Reset password link is sent successfully !</span></div>";
					
				} else {// email isexit else
					$_SESSION['error'] = 'You are not valid user';
				}
			} else { // username else
				$_SESSION['error'] = 'Please enter your email';
		}
// main if closed	
}

## Assign success or error msg to smarty variable and unset session variable
if($_SESSION['msg']!=''){
	$smarty->assign("successMsg", $_SESSION['msg']);
	unset($_SESSION['msg']);
}
if($_SESSION['error'] != ''){
	$smarty->assign("errorMsg", $_SESSION['error']);	
	unset($_SESSION['error']);
}


## Fetch footer logo and text setting
# Footer text
$footer_text = $configObj->getConfigValueById(2);
$smarty->assign('footer_text', $footer_text['config_value']);

# Footer logo
$footer_logo = $configObj->getConfigValueById(3);
$smarty->assign('footer_logo', $footer_logo['config_value']);

## Unset all the objects created which are on this page
unset($userObj);
unset($emailObj);
unset($adminObj);
unset($configObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'forgotpassword.tpl');
unset($smarty);
?>